package com.qidian.blog.servlet;

import com.qidian.blog.factory.ServiceFactory;
import com.qidian.blog.pojo.BlogArticle;
import com.qidian.blog.pojo.BlogArticleContent;
import com.qidian.blog.pojo.BlogCategory;
import com.qidian.blog.pojo.BlogTag;
import com.qidian.blog.service.BlogArticleService;
import com.qidian.blog.util.pojo.QDResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author 郭浩
 * @Date 2022/10/11 21:02
 */
@WebServlet("/article")
public class BlogArticleServlet extends BaseServlet {
    private BlogArticleService articleService = ServiceFactory.getService(BlogArticleService.class);
    public void edit(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        // 获取请求参数
        Long articleId = new Long(request.getParameter("articleId"));
        String articleTitle = request.getParameter("articleTitle");
        String articleSubTitle = request.getParameter("articleSubTitle");
        String articleIntroduction = request.getParameter("articleIntroduction");
        String articleCover = request.getParameter("articleCover");
        String articleContent = request.getParameter("articleContent");

        Long categoryId = 0L;
        try{categoryId = new Long(request.getParameter("categoryId"));}catch (NumberFormatException e){
            throw new RuntimeException("请求中的categoryId格式有误");
        }
        Integer status = 1;
        try{status = new Integer(request.getParameter("status"));}catch (NumberFormatException e){
            throw new RuntimeException("请求中的categoryId格式有误");
        }
        String [] targIds = request.getParameterValues("tag");

        // 创建对象
        BlogArticle article = new BlogArticle();
        article.setArticleId(articleId);
        article.setUpdated(new Date());
        article.setArticleCover(articleCover);
        article.setStatus(status);
        article.setArticleIntroduction(articleIntroduction);
        article.setArticleSubTitle(articleSubTitle);
        article.setArticleTitle(articleTitle);
        // 设置category属性
        BlogCategory cat = new BlogCategory();
        cat.setCategoryId(categoryId);
        article.setCategory(cat);
        // 设置tags
        List<BlogTag> tags = new ArrayList<>();
        if(targIds!=null) {
            for (String tagId : targIds) {
                BlogTag blogTag = new BlogTag();
                try {
                    blogTag.setTagId(new Long(tagId));
                    tags.add(blogTag);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    throw new RuntimeException("请求中的tagId格式有误");
                }
            }
        }
        // 将标签设置到article中。
        article.setTags(tags);
        BlogArticleContent content = new BlogArticleContent();
        content.setArticleContent(articleContent);
        // 保存
        QDResult result = articleService.update(article, content);
        respJson(result,response);
    }


    public void detail(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException{
        String remoteUser = req.getLocalAddr();
        Long articleId = null;
        try {
            articleId = new Long(req.getParameter("articleId"));
        }catch (NumberFormatException e){
            throw new RuntimeException("请求的articleId格式有误");
        }
        String token = req.getParameter("token");
        respJson(articleService.detail(articleId,token),resp);
    }
    public void doPraise(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException{
        Long articleId = null;
        try {
            articleId = new Long(req.getParameter("articleId"));
        }catch (NumberFormatException e){
            throw new RuntimeException("请求的articleId格式有误");
        }
        String token = req.getParameter("token");
        respJson(articleService.doPraise(articleId,token),resp);
    }

    public void save(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        // 获取请求参数
        String articleTitle = request.getParameter("articleTitle");
        String articleSubTitle = request.getParameter("articleSubTitle");
        String articleIntroduction = request.getParameter("articleIntroduction");
        String articleCover = request.getParameter("articleCover");
        String articleContent = request.getParameter("articleContent");

        Long categoryId = null;
        try{categoryId = new Long(request.getParameter("categoryId"));}catch (NumberFormatException e){
            throw new RuntimeException("请求中的categoryId格式有误");
        }
        String [] targIds = request.getParameterValues("tag");

        // 创建对象
        BlogArticle article = new BlogArticle();
        article.setArticleCover(articleCover);
        article.setArticleIntroduction(articleIntroduction);
        article.setArticleSubTitle(articleSubTitle);
        article.setArticleTitle(articleTitle);
        // 设置category属性
        BlogCategory cat = new BlogCategory();
        cat.setCategoryId(categoryId);
        article.setCategory(cat);
        // 设置tags
        List<BlogTag> tags = new ArrayList<>();
        if(targIds!=null) {
            for (String tagId : targIds) {
                BlogTag blogTag = new BlogTag();
                try {
                    blogTag.setTagId(new Long(tagId));
                    tags.add(blogTag);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    throw new RemoteException("请求中的tagId格式有误");
                }
            }
        }
        // 将标签设置到article中。
        article.setTags(tags);
        BlogArticleContent content = new BlogArticleContent();
        content.setArticleContent(articleContent);
        // 保存
        respJson(articleService.save(article,content),response);
    }

    public void list(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        int page = 1;
        int limit = 10;
        try{page = new Integer(request.getParameter("page"));}catch (NumberFormatException e){}
        try{limit = new Integer(request.getParameter("limit"));}catch (NumberFormatException e){}
        String articleTitle = request.getParameter("articleTitle");
        String timeStart = request.getParameter("timeStart");
        String timeEnd = request.getParameter("timeEnd");
        Long categoryId = 0L;
        try{categoryId = new Long(request.getParameter("categoryId"));}catch (NumberFormatException e){}
        BlogArticle article= new BlogArticle();
        article.setArticleTitle(articleTitle);
        BlogCategory category = new BlogCategory();
        category.setCategoryId(categoryId);
        article.setCategory(category);
        respJson(articleService.queryByPage(page,limit,article,timeStart,timeEnd),response);
    }
    public void get(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        Long id = 0L;
        try{id = new Long(request.getParameter("articleId"));}catch (NumberFormatException e){}
        respJson(articleService.queryById(id),response);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        Long id = 0L;
        try{id = new Long(request.getParameter("articleId"));}catch (NumberFormatException e){}
        BlogArticle blogArticle = new BlogArticle();
        blogArticle.setArticleId(id);
        blogArticle.setStatus(2);
        respJson(articleService.delete(blogArticle),response);
    }
    public void queryByCategory(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        Long categoryId = 0L;
        try{categoryId = new Long(request.getParameter("categoryId"));}catch (NumberFormatException e){}
        respJson(articleService.queryByCategoryId(categoryId),response);
    }

}
